package com.sunnyebuy.merchant.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.sunnyebuy.merchant.dao.MerchantBaseDAO;
import com.sunnyebuy.merchant.dao.MerchantDAO;
import com.sunnyebuy.merchant.po.Merchant;

/**
 * <p>
 * Title: MerchantDAOImpl.java
 * </p>
 * 
 * <p>
 * Description: com.sunnyebuy.merchant.dao.impl
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2008-2011 All rights reserved.
 * </p>
 * 
 * <p>
 * Created: 2011-12-28 上午11:42:57
 * </p>
 * 
 * @author Wenzhong Gu
 * 
 * @version 1.0
 */
@Repository(value = "merchantDAO")
public class MerchantDAOImpl extends MerchantBaseDAO implements MerchantDAO {

	@Override
	public Merchant findMerchant(String email, String password) {
		return this.getHibernateTemplate().execute(new HibernateCallback<Merchant>() {
			@Override
			public Merchant doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "FROM Merchant model WHERE model.merchantEmail=:email AND model.merchantPassword=:password";
				return (Merchant) session.createQuery(hql).uniqueResult();
			}
		});
	}

	@Override
	public Merchant loadMerchant(final String merchantId) {
		return this.getHibernateTemplate().execute(new HibernateCallback<Merchant>() {
			@Override
			public Merchant doInHibernate(Session session) throws HibernateException, SQLException {
				return (Merchant) session.load(Merchant.class, merchantId);
			}
		});
	}

	@Override
	public Merchant updateMerchant(final Merchant merchant) {
		return this.getHibernateTemplate().execute(new HibernateCallback<Merchant>() {
			@Override
			public Merchant doInHibernate(Session session) throws HibernateException, SQLException {
				session.merge(merchant);
				return merchant;
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ecshopster.merchant.dao.MerchantDAO#addMerchant(com.ecshopster.merchant.po.Merchant)
	 */
	@Override
	public Merchant addMerchant(final Merchant merchant) {
		return this.getHibernateTemplate().execute(new HibernateCallback<Merchant>() {
			public Merchant doInHibernate(Session session) throws HibernateException, SQLException {
				session.persist(merchant);
				return merchant;
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ecshopster.merchant.dao.MerchantDAO#findMerchantByEmail(java.lang.String)
	 */
	@Override
	public Merchant findMerchantByEmail(final String email) {
		return this.getHibernateTemplate().execute(new HibernateCallback<Merchant>() {
			public Merchant doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = "FROM Merchant model WHERE model.merchantEmail=:merchantEmail";
				return (Merchant) session.createQuery(hql).setParameter("merchantEmail", email).uniqueResult();
			}
		});
	}
}